package com.wisewin.pose.config;


import com.wisewin.pose.config.dto.ResultDTO;
import com.wisewin.pose.config.dto.ResultDTOBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 处理类似rest风格的接口，返回json
 */
@RestControllerAdvice
public class RestExceptionHandler{
    static final Logger log = LoggerFactory.getLogger(RestExceptionHandler.class);
    /**
     * 返回json
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    public ResultDTO restExceptionHandle(Exception e) {
        log.error("error:{}",e);
        return ResultDTOBuilder.failureMsg(e.getMessage());
    }

    /**
     * 参数效验失败
     * @param e
     * @return
     */
    @ExceptionHandler(value = ParamException.class)
    public ResultDTO paramExceptionHandle(Exception e) {
        //log.error("error:{}",e);
        return ResultDTOBuilder.failure("00001",e.getMessage());
    }

    /**
     * 参数效验失败
     * @param e
     * @return
     */
    @ExceptionHandler(value = LoginException.class)
    public ResultDTO LoginExceptionHandle(Exception e) {
        //log.error("error:{}",e);
        return ResultDTOBuilder.failure("00002",e.getMessage());
    }

    /**
     * 参数效验失败
     * @param e
     * @return
     */
    @ExceptionHandler(value = CouponsException.class)
    public ResultDTO CouponsException(Exception e) {
        //log.error("error:{}",e);
        return ResultDTOBuilder.failure("00003",e.getMessage());
    }

}